package com.yxx.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxx.mapper.SetMedicineMapper;
import com.yxx.mapper.WesternMedicineMapper;
import com.yxx.medicinecommon.constant.MessageConstant;
import com.yxx.medicinecommon.constant.StatusConstant;
import com.yxx.medicinecommon.exception.DeletionNotAllowedException;
import com.yxx.medicinecommon.exception.SetmedicineEnableFailedException;
import com.yxx.medicinecommon.result.PageResult;
import com.yxx.medicinepojo.dto.MedicinesSalesDTO;
import com.yxx.medicinepojo.dto.WesternMedicineDTO;
import com.yxx.medicinepojo.dto.WesternMedicinePageQueryDTO;
import com.yxx.medicinepojo.entity.SetMedicine;
import com.yxx.medicinepojo.entity.WesternMedicine;
import com.yxx.medicinepojo.vo.WesternMedicineVO;
import com.yxx.service.WesternMedicineService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author yuxiangxun
 * @date 2024/9/21
 * @apiNote
 */
@Service
public class WesternMedicineServiceImpl implements WesternMedicineService {

    @Autowired
    private WesternMedicineMapper westernMedicineMapper;
    @Autowired
    private SetMedicineMapper setMedicineMapper;
    /**
     * 西药分页查询
     * @param westernMedicinePageQueryDTO
     * @return
     */
    @Override
    public PageResult westernMedicinePage(WesternMedicinePageQueryDTO westernMedicinePageQueryDTO) {
        PageHelper.startPage(westernMedicinePageQueryDTO.getPage(),westernMedicinePageQueryDTO.getPageSize());
        Page<WesternMedicineVO> westernMedicineVOS = westernMedicineMapper.pageQueryWesternMedicine(westernMedicinePageQueryDTO);
        return new PageResult(westernMedicineVOS.getTotal(),westernMedicineVOS.getResult());

    }

    /**
     *
     * @param categoryId
     * @param name
     * @return
     */
    @Override
    public List<WesternMedicineVO> list(Long categoryId, String name) {
        WesternMedicine westernMedicine = WesternMedicine.builder()
                .status(StatusConstant.ENABLE)
                .categoryId(categoryId)
                .name(name)
                .build();

        WesternMedicineVO westernMedicineVO = new WesternMedicineVO();
        BeanUtils.copyProperties(westernMedicine,westernMedicineVO);
        List<WesternMedicineVO> list = westernMedicineMapper.list(westernMedicineVO);
        return list;
    }

    /**
     * 中药上架下架
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void StartOrStop(Integer status, Long id) {
        if(StatusConstant.DISABLE.equals(status)){
            List<SetMedicine> byChineseWesternMedicineByIdList = setMedicineMapper.getByChineseWesternMedicineById(id);
            if(byChineseWesternMedicineByIdList != null && byChineseWesternMedicineByIdList.size() > 0){
                byChineseWesternMedicineByIdList.forEach(setmedicine ->{
                    if(StatusConstant.ENABLE.equals(setmedicine.getStatus())){
                        throw new SetmedicineEnableFailedException(MessageConstant.WESTERN_MEDICINE_ENABLE_FAILED);
                    }
                });
            }
        }
        WesternMedicine westernMedicine = WesternMedicine.builder()
                .id(id)
                .status(status)
                .build();
        westernMedicineMapper.update(westernMedicine);
    }

    /**
     * 新增西药
     * @param westernMedicineDTO
     */
    @Override
    public void save(WesternMedicineDTO westernMedicineDTO) {
        WesternMedicine westernMedicine = new WesternMedicine();
        BeanUtils.copyProperties(westernMedicineDTO,westernMedicine);
        westernMedicineMapper.insert(westernMedicine);
    }

    /**
     * 修改西药
     */
    @Override
    public void updateWesternMedicine(WesternMedicineDTO westernMedicineDTO) {
        WesternMedicine westernMedicine = new WesternMedicine();
        BeanUtils.copyProperties(westernMedicineDTO,westernMedicine);
        westernMedicineMapper.update(westernMedicine);
    }

    @Override
    public WesternMedicineVO WesternMedicineSelectUpdateById(Long id) {
        WesternMedicine westernMedicine = westernMedicineMapper.WesternMedicineSelectUpdateById(id);
        WesternMedicineVO westernMedicineVO = new WesternMedicineVO();
        BeanUtils.copyProperties(westernMedicine,westernMedicineVO);
        return westernMedicineVO;
    }

    /***
     * 西药批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWesternMedicine(List<Long> ids) {
        for(Long id : ids){
            WesternMedicine westernMedicine = westernMedicineMapper.WesternMedicineById(id);
            if(StatusConstant.ENABLE.equals(westernMedicine.getStatus())){
                throw new DeletionNotAllowedException(MessageConstant.WESTERN_MEDICINE_ON_SALE);
            }
        }

        List<Long> setmedicineID_MEdicine = setMedicineMapper.setMedicineById(ids);
        if(setmedicineID_MEdicine != null && setmedicineID_MEdicine.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.WESTERN_CATEGORY_BE_RELATED_BY_SETMEDICINE_MEDICINE);
        }

        westernMedicineMapper.deleteByIds(ids);
    }


}
